package com.chj;

import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.web.support.SpringBootServletInitializer;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.kafka.annotation.EnableKafka;
import org.springframework.stereotype.Controller;
import org.springframework.web.WebApplicationInitializer;
import org.springframework.web.bind.annotation.RequestMapping;

import com.chj.utils.ApplicationContextUtil;

@SpringBootApplication
@EnableKafka
@EnableAutoConfiguration
public class KafkaProducerConsumer2AppMain extends SpringBootServletInitializer implements WebApplicationInitializer {

	static Logger log = LoggerFactory.getLogger(KafkaProducerConsumer2AppMain.class);

	public static void main(String[] args) {
		ConfigurableApplicationContext ac = SpringApplication.run(KafkaProducerConsumer2AppMain.class, args);
		ApplicationContextUtil.setApplicationContext(ac);
		// final TestResource test = ac.getBean(TestResource.class);
		// new Thread(new Runnable() {
		//
		// @Override
		// public void run() {
		// while (true) {
		// test.sendKafka("test", "test-key", "test-message:" + UUID.randomUUID().toString());
		// try {
		// Thread.currentThread().sleep(100);
		// } catch (Exception e) {
		// e.printStackTrace();
		// }
		// }
		// }
		// }).start();
	}

	@Override
	protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) {
		return builder.sources(getClass());
	}

	@Controller
	public class RootController {

		@RequestMapping("/")
		public String index(HttpServletRequest request) {
			return "redirect:/swagger-ui.html";
		}
	}

}
